<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use kartik\file\FileInput;
use yii\web\JsExpression;
use common\models\business\BusinessContractStatements;
use backend\assets\PrintAsset;
PrintAsset::register($this);
/* @var $this yii\web\View */
/* @var $model common\models\business\BusinessContractStatements */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="row">
    <div class="col-lg-12">
        <section class="panel">
        <div class="panel-body">
            <?php $form = ActiveForm::begin([
                'options' => ['class' => 'form-horizontal','enctype' => 'multipart/form-data'],
                'fieldConfig' => [
                    'template' => "<div class='col-xs-3 col-sm-2 text-right'>{label}</div><div class='col-xs-9 col-sm-7'>{input}</div><div class='col-xs-12 col-xs-offset-3 col-sm-3 col-sm-offset-0'>{error}</div>",
                ]
            ]); ?>

            <div class="adv-table editable-table ">
                <div class="business-contract-statements-form">


                        <?= $form->field($model, 'number')->textInput(['maxlength' => true,'value'=>$model->isNewRecord?date('YmdHis').rand(10000,99999):$model->number,'readonly'=>true]) ?>

                        <?= $form->field($model, 'contract_id')->widget(\kartik\select2\Select2::className(), [
                            'options' => ['placeholder' => '请输入合同名称 ...'],
                            'initValueText'=>!$model->contract?'':$model->contract->name,
                            'pluginOptions' => [
                                'disabled' => $model->isNewRecord?false:true,
                                'allowClear' => true,
                                'minimumInputLength' => 1,//重要
                                'language' => [
                                    'errorLoading' => new JsExpression("function () { return 'Waiting...'; }"),
                                ],
                                'ajax' => [
                                    'url' => '/business/search/search-contract',
                                    'dataType' => 'json',
                                    'data' => new JsExpression('function(params) { return {q:params.term}; }'),
                                    'cache' => true
                                ],
                                'width' => '400px',
                                'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
                                'templateResult' => new JsExpression('function(res) { return res.text; }'),
                                'templateSelection' => new JsExpression('function (res) { return res.text; }'),
                            ],
                        ]);
                        ?>

                        <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>

                        <?= $form->field($model, 'connect_person')->textInput(['maxlength' => true]) ?>

                        <?= $form->field($model, 'connect_tel')->textInput(['maxlength' => true]) ?>

                        <?= $form->field($model, 'connect_address')->textInput(['maxlength' => true]) ?>


                        <?= $form->field($model, 'ispay')->dropDownList(BusinessContractStatements::$pays) ?>


                        <?= $form->field($model, 'remark')->textarea(['maxlength' => true]) ?>


                        <?= $form->field($model, 'file[]')->widget(FileInput::classname(), [
                            'options' => ['multiple' => true],
                            'pluginOptions' => [
                                // 需要预览的文件格式
                                'previewFileType' => 'image',
                                // 预览的文件
                                'initialPreview' => \common\components\Upload::checkGetUrl($model->file,true,'file',false),
                                // 需要展示的图片设置，比如图片的宽度等
                                'initialPreviewConfig' => ['width' => '120px'],
                                // 是否展示预览图
                                'initialPreviewAsData' => true,
                                'showUpload' => false,
                            ],
                        ]); ?>
                </div>
            </div>


            <!--购进单-->
            <div class="row">
                <div class="col-md-10 col-md-offset-1">
                    <table class="table table-striped" id="cost">
                        <tr>
                            <th style="width: 10%">购进单号</th>
                            <th style="width: 10%">金额</th>
                            <th style="width: 10%">数量</th>
                            <th style="width: 10%">时间</th>
                            <th style="width: 10%">状态</th>
                            <th style="width: 20%">备注</th>
                            <th style="width: 10%">操作</th>
                        </tr>
                        <?php $total=0; ?>
                        <?php foreach ($model->purchas as $key => $row) { ?>
                            <tr class="gcostdata">
                                <input type="hidden" name="uppurid[]" value="<?= $row['id'] ?>">
                                <td><input type="text" name="number[]" class="form-control" value="<?= $row['number'] ?>" readonly=true></td>
                                <td><input type="number" name="price[]" class="form-control" value="<?= $row['price'] ?>" readonly=true></td>
                                <td><input type="number" name="num[]"  class="form-control" value="<?= $row['num'] ?>" readonly=true></td>
                                <td><input type="text"  class="form-control" name="createtime[]" value="<?= date('Y-m-d',$row['createtime'])?>"  readonly=true></td>
                                <td><input type="text" name="status[]"  class="form-control" value="<?= BusinessContractStatements::$statues[$row['status']] ?>" disabled=true></td>
                                <td><input type="text" name="remark[]"  class="form-control" value="<?= $row['remark'] ?>" readonly=true></td>
                                <td>
                                    <div style="float: left;width: 13%;margin-left: 1%">
                                        <?= Html::button('删除此项',['class'=>'btn btn-warning btn-sm del']) ?>
                                    </div>
                                </td>
                            </tr>
                        <?php } ?>
                        <tr class="gcostdata" id="zongji">
                            <td><b>总计</b></td>
                            <td><b><?= $total ?></b></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                        </tr>
                    </table>
                    <div class="form-group">
                        <?= Html::button('添加购进单',['id'=>'ccopy','class'=>'btn btn-info']) ?>
                    </div>
                </div>
            </div>


            <!--调整单-->
            <div class="row">
                <div class="col-md-10 col-md-offset-1">
                    <table class="table table-striped" id="cost">
                        <tr>
                            <th style="width: 10%">调整单号</th>
                            <th style="width: 10%">金额</th>
                            <th style="width: 10%">数量</th>
                            <th style="width: 10%">时间</th>
                            <th style="width: 20%">备注</th>
                            <th style="width: 10%">操作</th>
                        </tr>
                        <?php $total=0; ?>
                        <?php foreach ($model->adjustment as $key => $row) { ?>
                            <tr class="costdata">
                                <input type="hidden" name="upajid[]" value="<?= $row['id'] ?>">
                                <td><input type="text" name="anumber[]" class="form-control" value="<?= $row['number'] ?>" readonly=true></td>
                                <td><input type="number" name="aprice[]" class="form-control" value="<?= $row['price'] ?>" readonly=true></td>
                                <td><input type="number" name="anum[]"  class="form-control" value="<?= $row['num'] ?>" readonly=true></td>
                                <td><input type="text"  class="form-control" name="createtime[]" value="<?= date('Y-m-d',$row['createtime'])?>"  readonly=true></td>
                                <td><input type="text" name="aremark[]"  class="form-control" value="<?= $row['remark'] ?>" readonly=true></td>
                                <td>
                                    <div style="float: left;width: 13%;margin-left: 1%">
                                        <?= Html::button('删除此项',['class'=>'btn btn-warning btn-sm del']) ?>
                                    </div>
                                </td>
                            </tr>
                        <?php } ?>
                        <tr class="costdata" id="zongji">
                            <td><b>总计</b></td>
                            <td><b><?= $total ?></b></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                        </tr>
                    </table>


                    <div class="form-group">
                        <?= Html::button('添加调整单',['id'=>'acopy','class'=>'btn btn-info']) ?>
                        <?= Html::submitButton($model->isNewRecord ? '确认提交' : '确认更新', ['class' => $model->isNewRecord ? 'btn btn-success sub' : 'btn btn-primary sub']) ?>
                    </div>

                </div>
            </div>
            <?php ActiveForm::end(); ?>
        </div>
    </section>
</div>
</div>




<!--选择购进单模态窗-->
<div class="modal fade" id="kucun" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="kucuntitle">购进单</h4>
            </div>
            <div class="modal-body">

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="gsavekucun">保存</button>
            </div>
        </div>
    </div>
</div>




<!--选择调整单模态窗-->
<div class="modal fade" id="ajest" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="kucuntitle">调整单</h4>
            </div>
            <div class="modal-body">

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="savekucun">保存</button>
            </div>
        </div>
    </div>
</div>



<script>
    <?php $this->beginBlock('js_end') ?>
        $(document).ready(function() {


            //购进单的选择的数据，防止前端多次选择库存相同的产品
            var selkudata = [];
            var selajekudata = [];

            //添加购进单
            $('#ccopy').on('click',function (){
                var contract_id = $('#businesscontractstatements-contract_id').val();
                if(contract_id == ''){
                    alert('请选择要结算的合同');
                    return false;
                }
                $.post('/business/contract-statements/search-purchase',{selid:selkudata,contract_id:contract_id},function(html){
                    $('#kucun .modal-body').html(html);
                    $('#kucun').modal({
                        keyboard: false
                    })
                })
            });

            //保存选择的购进单
            $('#gsavekucun').click(function(){
                var data = $('#kucunsel').val();
                if(data === null){
                    //没有选关闭窗口
                    $('#kucun').modal('hide');
                    return false;
                }
                //合并此页面已经选择的猪，为了防止重复添加
                selkudata = selkudata.concat(data);
                //选择了，去请求
                $.post('/business/contract-statements/get-purchase',{data:data},function(html){
                    var str = '';
                    $(html).each(function(idnex,res){
                        str += '<tr class="gcostdata"><td><input type="hidden" name="purid[]" value="'+res.id+'"><input type="text" name="number[]" class="form-control" value="'+res.number+'" disabled=true></td> <td><input type="text" name="price[]" class="form-control" value="'+res.price+'" readonly=true></td> <td><input type="number" name="num[]"  class="form-control" value="'+res.num+'" readonly=true></td> <td><input type="text"  class="form-control" name="createtime[]" value="'+res.createtime+'" disabled=true></td> <td><input type="text" name="status[]"  class="form-control" value="'+res.status+'" disabled=true></td> <td><input type="text" name="remark[]"  class="form-control" value="'+res.remark+'" disabled=true></td> <td> <div style="float: left;width: 13%;margin-left: 1%"> <?= Html::button('删除此项',['class'=>'btn btn-warning btn-sm del']) ?> </div> </td></tr>';
                    });
                    $('#kucun').modal('hide');
                    $('.gcostdata').last().before(str);
                },'json');
            });


            //添加调整单
            $('#acopy').on('click',function (){
                var contract_id = $('#businesscontractstatements-contract_id').val();
                if(contract_id == ''){
                    alert('请选择要结算的合同');
                    return false;
                }
                $.post('/business/contract-statements/search-ajest',{selid:selajekudata,contract_id:contract_id},function(html){
                    $('#ajest .modal-body').html(html);
                    $('#ajest').modal({
                        keyboard: false
                    })
                })
            });


            //保存选择的调整单
            $('#savekucun').click(function(){
                var data = $('#akucunsel').val();
                if(data === null){
                    //没有选关闭窗口
                    $('#ajest').modal('hide');
                    return false;
                }
                //合并此页面已经选择的猪，为了防止重复添加
                selajekudata = selajekudata.concat(data);
                //选择了，去请求
                $.post('/business/contract-statements/get-ajest',{data:data},function(html){
                    var str = '';
                    $(html).each(function(idnex,res){
                        str += '<tr class="costdata"><td><input type="hidden" name="upajid[]" value="'+res.id+'"><input type="text" name="anumber[]" class="form-control" value="'+res.number+'" disabled=true></td> <td><input type="text" name="aprice[]" class="form-control" value="'+res.price+'" readonly=true></td> <td><input type="number" name="anum[]"  class="form-control" value="'+res.num+'" readonly=true></td> <td><input type="text"  class="form-control" name="acreatetime[]" value="'+res.createtime+'" disabled=true></td><td><input type="text" name="aremark[]"  class="form-control" value="'+res.remark+'" disabled=true></td> <td> <div style="float: left;width: 13%;margin-left: 1%"> <?= Html::button('删除此项',['class'=>'btn btn-warning btn-sm del']) ?> </div> </td></tr>';
                    });
                    $('#ajest').modal('hide');
                    $('.costdata').last().before(str);
                },'json');
            });



            //删除一项
            $('#cost').on('click','.del',function () {
                if (confirm('确定要删除吗？')) {
                    $(this).parent().parent().parent('.costdata').remove();
                }
            });



        });

    <?php $this->endBlock(); ?>
    <?php $this->registerJs($this->blocks['js_end'],\yii\web\View::POS_END);//将编写的js代码注册到页面底部 ?>
</script>
